package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.TeachingBook;
import com.ruoyi.personnel.service.ITeachingBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 教程编写Controller
 *
 * @author ruoyi
 * @date 2021-03-05
 */
@Api("教程编写")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/teaching/book" )
public class TeachingBookController extends BaseController {

    private final ITeachingBookService iTeachingBookService;

    /**
     * 查询教程编写列表
     */
    @ApiOperation("查询教程编写列表")
    @PreAuthorize("@ss.hasPermi('teaching:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeachingBook teachingBook) {
        startPage();
        List<TeachingBook> list = iTeachingBookService.queryList(teachingBook);
        return getDataTable(list);
    }

    /**
     * 导出教程编写列表
     */
    @ApiOperation("导出教程编写列表")
    @PreAuthorize("@ss.hasPermi('teaching:book:export')" )
    @Log(title = "教程编写" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TeachingBook teachingBook) {
        List<TeachingBook> list = iTeachingBookService.queryList(teachingBook);
        ExcelUtil<TeachingBook> util = new ExcelUtil<TeachingBook>(TeachingBook.class);
        return util.exportExcel(list, "book" );
    }

    /**
     * 获取教程编写详细信息
     */
    @ApiOperation("获取教程编写详细信息")
    @PreAuthorize("@ss.hasPermi('teaching:book:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iTeachingBookService.getById(id));
    }

    /**
     * 新增教程编写
     */
    @ApiOperation("新增教程编写")
    @PreAuthorize("@ss.hasPermi('teaching:book:add')" )
    @Log(title = "教程编写" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeachingBook teachingBook) {
      teachingBook.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iTeachingBookService.save(teachingBook) ? 1 : 0);
    }

    /**
     * 修改教程编写
     */
    @ApiOperation("修改教程编写")
    @PreAuthorize("@ss.hasPermi('teaching:book:edit')" )
    @Log(title = "教程编写" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeachingBook teachingBook) {
        return toAjax(iTeachingBookService.updateById(teachingBook) ? 1 : 0);
    }

    /**
     * 删除教程编写
     */
    @ApiOperation("删除教程编写")
    @PreAuthorize("@ss.hasPermi('teaching:book:remove')" )
    @Log(title = "教程编写" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iTeachingBookService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
